package com.gjy.concurrency.h1;

import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.UUID;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-05-30 09:05:11
 */
public class CreateThread {

    private static final Logger logger = LoggerFactory.getLogger(CreateThread.class);

    //    private static final LongAdder adder = new LongAdder();

    @Test
    public void test5() throws ExecutionException, InterruptedException {
        ExecutorService pool = Executors.newFixedThreadPool(3);

        Future<String> submit = pool.submit(() -> UUID.randomUUID().toString());

        pool.shutdown();

        System.out.println(submit.get());
    }

    @Test
    public void test4() throws ExecutionException, InterruptedException {
        FutureTask<String> task = new FutureTask<>(new ImplCallable());

        new Thread(task).start();

        String s = task.get();
        logger.info("s = {}", s);
    }

    private static final class ImplCallable implements Callable<String> {

        @Override
        public String call() throws Exception {
            return UUID.randomUUID().toString();
        }
    }

    @Test
    public void test3() {
        Thread thread = new Thread(new ImplRunnable());

        thread.start();
    }

    private static final class ImplRunnable implements Runnable {

        @Override
        public void run() {
            logger.info("ImplRunnable = {}", Thread.currentThread().getName());
        }

    }

    @Test
    public void test2() {
        Thread t;
        for (int i = 0; i < 3; i++) {
            t = new ExtendsThread();
            t.start();
        }
    }

    private static final AtomicInteger adder = new AtomicInteger();

    private static final class ExtendsThread extends Thread {
        public ExtendsThread() {
            super("ExtendsThread-" + adder);
        }

        @Override
        public void run() {
            logger.info(Thread.currentThread().getName() + "-" + adder.incrementAndGet());
        }
    }

    @Test
    public void test1() {
        // Instantiating a 'Thread' with default 'run()' method
        Thread thread = new Thread();
        logger.info("thread name = {}", thread.getName());
        logger.info("thread id = {}", thread.getId());
        thread.start();
    }
}
